﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Configuration;

namespace ProjetLabo.Class.Utils
{
    class CurrentProjectSingleton
    {
        private string projectType;  
        private string SGBDType;
        private ConnectionString connectionString;

        private static CurrentProjectSingleton instance = null;

        public const string PROJECTTYPE_ASP = "ASP.NET";
        public const string PROJECTTYPE_WPF = "Windows Presentation Foundation";
        public const string PROJECTTYPE_PHONE = "Windows Phone";
        public const string PROJECTTYPE_W8 = "Windows 8";

        public const string SGBDTYPE_MYSQL = "MySQL";
        public const string SGBDTYPE_POSTGR = "PostgreSQL";
        public const string SGBDTYPE_SQLITE = "SQLite";
        public const string SGBDTYPE_ORACLE = "Oracle Database";


        public CurrentProjectSingleton()
        {
            this.ConnectionString = new ConnectionString();
        }

        public static CurrentProjectSingleton getInstance
        {
            
            get{
                if(instance == null)
                    instance = new CurrentProjectSingleton();

                return instance;
            }
        }

        public string ProjectType
        {
            get { return projectType; }
            set { projectType = value; }
        }

        public string SGBDType1
        {
            get { return SGBDType; }
            set { SGBDType = value; }
        }

        
        internal ConnectionString ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }

        public static void updateConnectionString(String conn)
        {
            XmlDocument doc = new XmlDocument();
            String path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            doc.Load(path);

            foreach (XmlElement element in doc.DocumentElement)
            {
                if (element.Name == "connectionStrings")
                    element.FirstChild.Attributes[1].Value = conn;
            }

            doc.Save(path);
        }

        public string toString()
        {
            return "Project type: "
                    + this.projectType
                    + "\nSGBD type: "
                    + this.SGBDType
                    +"\n**********\nConnectionString:\n"
                    +this.ConnectionString.getConnectionString();

        }

    }
}

